package com.nexgo.payment.action.inputpin;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.nexgo.payment.R;
import com.nexgo.payment.action.Action;
import com.nexgo.payment.action.ActionResult;
import com.nexgo.payment.action.ActionStep;
import com.nexgo.payment.action.OnActionListener;
import com.nexgo.payment.constrant.SysConstant;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.entity.TransData;
import com.nexgo.payment.ui.BaseFragment;
import com.nexgo.payment.util.StringUtils;
import com.nexgo.payment.widget.TitleBuilder;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class InputPinFragment extends BaseFragment implements Action<ActionStep>, InputPinContract.View, BaseFragment.DownTimerListener {

    public static final String TRANS_DATA = "trans_data";
    public static final String IS_ONLINE_PIN = "is_online_pin";
    public static final String LEFT_TIMES = "left_times";

    @BindView(R.id.tv_amount)
    TextView tvAmount;
    @BindView(R.id.tv_amount_format)
    TextView tvAmountFormat;
    @BindView(R.id.tv_pls_input_pin)
    TextView tvPlsInputPin;
    @BindView(R.id.tv_input_pin)
    TextView tvInputPin;
    @BindView(R.id.tv_left_times)
    TextView tvLeftTimes;
    @BindView(R.id.title_tv_right)
    TextView tvTitleRight;
    private Unbinder unbinder;
    private InputPinContract.Presenter mPresenter;
    private OnActionListener mListener;
    private TransData mTransData;
    private boolean mIsOnlinePin;
    private int mLeftTimes;

    public InputPinFragment() {
    }

    public static InputPinFragment newInstance(TransData transData, boolean isOnlinePin, int leftTimes) {
        InputPinFragment inputPinFragment = new InputPinFragment();
        Bundle b = new Bundle();
        b.putParcelable(TRANS_DATA, transData);
        b.putBoolean(IS_ONLINE_PIN, isOnlinePin);
        b.putInt(LEFT_TIMES, leftTimes);
        inputPinFragment.setArguments(b);
        return inputPinFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        //强制屏幕常亮
        Window win = getActivity().getWindow();
        win.setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//        //屏蔽菜单键
//        win.addFlags(5);
//        //屏蔽home键
//        win.addFlags(3);
//        //屏蔽下拉
//        Settings.System.putInt(getContext().getContentResolver(), "status_bar_disabled", 1);

        mTransData = null;
        mIsOnlinePin = false;
        mLeftTimes = 0;
        Bundle bundle = getArguments();
        if (bundle != null) {
            mTransData = bundle.getParcelable(TRANS_DATA);
            mIsOnlinePin = bundle.getBoolean(IS_ONLINE_PIN);
            mLeftTimes = bundle.getInt(LEFT_TIMES);
        }
        new InputPinPresenter(this, Repository.getInstance(), mTransData, mIsOnlinePin);
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fragment_input_pin, container, false);
        unbinder = ButterKnife.bind(this, view);

        initTitle(view);

        return view;
    }

    //初始化标题栏
    private void initTitle(View view) {
        new TitleBuilder(view)
                .setExternalTitleBgRes(R.drawable.bg_blue_icon)
                .setLeftImage(android.R.drawable.ic_secure)
                .setTitleText(getString(R.string.nexgo_secure_keyboard))
                .build();
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {
        if (mTransData != null && !TextUtils.isEmpty(mTransData.getAmount())) {
            tvAmount.setVisibility(View.VISIBLE);
            tvAmountFormat.setVisibility(View.VISIBLE);
            String format = SysConstant.FUND_SYMBOL + StringUtils.formatAmount(mTransData.getAmount(), SysConstant.FUND_DIGITS);
            tvAmountFormat.setText(format);
        }
        tvPlsInputPin.setText(getString(R.string.input_pin, getString(mIsOnlinePin ? R.string.online : R.string.offline)));
        if (mLeftTimes == 0) {
            tvLeftTimes.setVisibility(View.GONE);
        } else {
            tvLeftTimes.setText(getString(R.string.pin_left_times, String.valueOf(mLeftTimes)));
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnActionListener) {
            mListener = (OnActionListener) context;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.start();
    }

    @Override
    public void onStop() {
        super.onStop();
        mPresenter.destroy();
    }

    @Override
    public ActionStep getActionStep() {
        return ActionStep.INPUT_PIN;
    }

    @Override
    public void setPresenter(InputPinContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void showCountTime(int countTime) {
        setDownTimer(countTime, this);
    }

    @Override
    public void showPinMask() {
        tvInputPin.append("* ");
    }

    @Override
    public void showPinCancel() {
        if (mListener != null) {
            mListener.setActionResult(new ActionResult(this, ActionResult.ERR_CANCEL, null));
        }
    }

    @Override
    public void showPinClear() {
        tvInputPin.setText("");
    }

    @Override
    public void showPinConfirm(byte[] pin) {
        if (mListener != null) {
            mListener.setActionResult(new ActionResult(this, ActionResult.SUCC, pin));
        }
        cancelDownTimer();
    }

    @Override
    public void showPinTimeout() {
        if (mListener != null) {
            mListener.setActionResult(new ActionResult(this, ActionResult.ERR_TIMEOUT, getString(R.string.input_pin_time_out)));
        }
    }

    @Override
    public void showPinError() {
        if (mListener != null) {
            mListener.setActionResult(new ActionResult(this, ActionResult.ERR_FAIL, null));
        }
    }

    @Override
    public void showForcePin() {
        tvInputPin.setHint(getString(R.string.force_input_trans_pin));
    }

    @Override
    public boolean isActive() {
        return isAdded();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        mPresenter.destroy();
//        Settings.System.putInt(getContext().getContentResolver(), "status_bar_disabled", 0);
        getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    @Override
    public void onTick(long secondsUntilFinished) {
        if (!isActive()) {
            return;
        }
        tvTitleRight.setVisibility(View.VISIBLE);
        tvTitleRight.setText(String.valueOf(secondsUntilFinished));
    }

    @Override
    public void onFinish() {
        if (isActive()) {
            tvTitleRight.setText("0");
           /* if (mListener != null) {
                mListener.setActionResult(new ActionResult(this, ActionResult.ERR_TIMEOUT, getString(R.string.input_pin_time_out)));
            }*/
        }
    }
}
